package cn.acewill.yxd.yunxundian.ui.sign;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import cn.acewill.yxd.yunxundian.BaseActivity;
import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.adapter.QuestionAdapter;
import cn.acewill.yxd.yunxundian.model.wentifenxiBean;
import cn.acewill.yxd.yunxundian.utils.CacheActivity;

/**
 * Created by XXX on 2017/6/1.
 */

public class WenTiFenXiListActivity extends BaseActivity{
    protected YXDAPP yxdapp=new YXDAPP();
    protected DbManager db = x.getDb(yxdapp.getDaoConfig());
    private ListView addListView;
    private LinearLayout nodata;
    //保存查询出来的list;
    private List<wentifenxiBean> taskList=new ArrayList<>();
    private QuestionAdapter adapter;
    private int schid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.taskpalnlist_view);
        initView();
        CacheActivity.addActivity(this);
    }
    public void initView(){
        schid=getIntent().getIntExtra("schid",0);
        Toolbar myToolBar = (Toolbar) findViewById(R.id.myToolBar);
        myToolBar.setTitle("");
        myToolBar.setNavigationIcon(R.mipmap.back);
        setSupportActionBar(myToolBar);
        myToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        this.addListView= (ListView) findViewById(R.id.addListView);
        this.nodata= (LinearLayout) findViewById(R.id.nodata);
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadLocalData();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_task_wentifenxi,menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.addquestion:
                startActivity(new Intent(this,TaskQuestonListActivity.class).putExtra("schid",schid));
                break;
            case R.id.doplan:
                if (taskList==null||taskList.size()==0){
                    BaseActivity.showAnimaDialog(this,"当前无问题分析数据，请先添加问题分析");
                }else{
                    startActivity(new Intent(this,SendTaskPlanActivity.class).putExtra("schid",schid));
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    //查询本地数据库
    public void loadLocalData(){
        try {
            taskList=db.selector(wentifenxiBean.class).where("schid","=",schid).findAll();
            if (taskList==null||taskList.size()==0){
                nodata.setVisibility(View.VISIBLE);
                addListView.setVisibility(View.GONE);
            }else{
                nodata.setVisibility(View.GONE);
                addListView.setVisibility(View.VISIBLE);
                adapter=new QuestionAdapter(this, taskList, new QuestionAdapter.onClickCallBack() {
                    @Override
                    public void deleteQuest(int position) {
                        wentifenxiBean bean=taskList.get(position);
                        try {
                            db.delete(bean);//删除数据
                            taskList.remove(position);
                            adapter.notifyDataSetChanged();
                        } catch (DbException e) {
                            e.printStackTrace();
                        }
                    }
                });
                addListView.setAdapter(adapter);
                addListView.setAdapter(adapter);
                adapter.notifyDataSetChanged();

            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case 101:
                //重新加载数据
              loadLocalData();
                break;
        }
    }
}
